; package-installer.nsi
;

;--------------------------------
; Definitions

!include "LogicLib.nsh"
!include "MUI2.nsh" 

!ifdef DBG
	!define DBG_MARKER	"_debug"
	!define DBG_LETTER	"d"
	!define INSTALLSIZE	374091776
!else	
	!define DBG_MARKER	""
	!define DBG_LETTER	""
	!define INSTALLSIZE	43421696
!endif


!define PRODUCT_BASE	"cad-play-ground"
!define PRODUCT_NAME	"${PRODUCT_BASE}${DBG_MARKER}"
!define COMPANYNAME		"TNick"
!define VERSIONMAJOR	"1"
!define VERSIONMINOR	"0"
!define VERSIONBUILD	"0"

!define PRODUCT_VERSION	"${VERSIONMAJOR}.${VERSIONMINOR}"
!define HELPURL			"http://sites.google.com/site/cadplayground/online-help"
!define UPDATEURL		"http://sites.google.com/site/cadplayground/online-help"
!define ABOUTURL		"http://sites.google.com/site/cadplayground"
!define DESCRIPTION		"Package to facilitate a form of literate programming."

!define MUI_WELCOMEPAGE_TEXT			'The ${PRODUCT_NAME} \
project aims to create a set of tools that facilitate a form of \
literate programming. It currently include a stand alone editor based \
on Qt. \
\
'

!define SETUP_NAME "..\..\distrib\windows\${PRODUCT_NAME}-${PRODUCT_VERSION}-setup.exe"
!define FULL_NAME "${PRODUCT_NAME} ${PRODUCT_VERSION}"
!define REG_UNINST "Software\Microsoft\Windows\CurrentVersion\Uninstall"


;================================



;--------------------------------
; Basic stuff

; The name of the installer
Name "${FULL_NAME}"

; The file to write
OutFile "${SETUP_NAME}"

; The default installation directory
InstallDir $PROGRAMFILES\${PRODUCT_NAME}

; The text to prompt the user to enter a directory
DirText "This will install ${FULL_NAME} on your computer. Choose a directory:"

ShowInstDetails show
ShowUnInstDetails show

;SetCompressor /SOLID lzma
;SetCompressorDictSize 12
SetCompressor /SOLID ZLIB

;Request application privileges for Windows Vista 
RequestExecutionLevel user
;Could be 'admin'


!define MUI_WELCOMEPAGE_TITLE			'Welcome to the installer program for ${FULL_NAME}'
!define MUI_INNERTEXT_LICENSE_BOTTOM	'If you press Cancel the install will be aborted.'
!define MUI_TEXT_LICENSE_TITLE			'License'
!define MUI_TEXT_LICENSE_SUBTITLE		'Boring legal stuff.'
!define MUI_INNERTEXT_LICENSE_TOP		'Modified (3 clause) BSD License:'
!define MUI_TEXT_DIRECTORY_TITLE		'Program location:'
!define MUI_TEXT_DIRECTORY_SUBTITLE		' '
!define MUI_TEXT_INSTALLING_TITLE		'Install in progress'
!define MUI_TEXT_INSTALLING_SUBTITLE	'Files are being copied to your computer'
!define MUI_TEXT_FINISH_TITLE			'Install completed'
!define MUI_TEXT_FINISH_SUBTITLE		'The program was installed and is ready to use.'
!define MUI_TEXT_ABORT_TITLE			'Aborting installation'
!define MUI_TEXT_ABORT_SUBTITLE			' '


!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "..\..\COPYING"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
 
 
!insertmacro MUI_LANGUAGE "English"

;================================


Function .onInit
 
  ReadRegStr $R0 HKLM "${REG_UNINST}\${PRODUCT_NAME}" "UninstallString"  
  StrCmp $R0 "" done
 
  MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
  "${PRODUCT_NAME} is already installed. $\n$\nClick `OK` to remove the \
  previous version or `Cancel` to cancel this upgrade." \
  IDOK uninst
  Abort
 
;Run the uninstaller
uninst:

  ClearErrors
  ExecWait '$R0 _?=$INSTDIR' ;Do not copy the uninstaller to a temp file
 
  IfErrors no_remove_uninstaller done
  no_remove_uninstaller:
  MessageBox MB_OK|MB_ICONEXCLAMATION "Could not remove previous version!"
  Abort    
 
done:
 
FunctionEnd
  
  
;--------------------------------
; The stuff to install

Section			"Installer"


; Set output path to the installation directory.
SetOutPath $INSTDIR

; files in install folder
File ..\..\distrib\${PRODUCT_BASE}-${PRODUCT_VERSION}.tar.gz

File ..\..\build\assistant.exe
File ..\..\build\libgcc_s_dw2-1.dll
File ..\..\build\libcpg.a
File ..\..\build\mingwm10.dll
File ..\..\build\libstdc++-6.dll

File ..\..\build\CadPlayG${DBG_MARKER}.exe
File ..\..\build\cpg${DBG_MARKER}.dll


File ..\..\build\QtWebKit4.dll
File ..\..\build\QtCLucene4.dll

!ifdef DBG
; these are needed by the assistant only 

File ..\..\build\QtHelp4.dll
File ..\..\build\QtSql4.dll
File ..\..\build\QtCore4.dll
File ..\..\build\QtGui4.dll
File ..\..\build\QtNetwork4.dll


; these are needed by our applications in debug mode
File ..\..\build\QtCore${DBG_LETTER}4.dll
File ..\..\build\QtGui${DBG_LETTER}4.dll
File ..\..\build\QtNetwork${DBG_LETTER}4.dll
File ..\..\build\QtWebKit${DBG_LETTER}4.dll

!else	
File ..\..\build\QtCore${DBG_LETTER}4.dll
File ..\..\build\QtGui${DBG_LETTER}4.dll
File ..\..\build\QtHelp${DBG_LETTER}4.dll
File ..\..\build\QtNetwork${DBG_LETTER}4.dll
File ..\..\build\QtSql${DBG_LETTER}4.dll
!endif


File ..\..\COPYING
File ..\..\NEWS

; Set output path to the help directory.
SetOutPath $INSTDIR\help

File ..\..\build\qt_docs\cpg.qhc


; Set output path to the lang directory.
SetOutPath $INSTDIR\lang

File ..\Qt\editor\CadPlayG_ro_RO.qm
File ..\Qt\library-dynamic\dyn_cpg_ro_RO.qm
File ..\Qt\library-static\sta_cpg_ro_RO.qm



; Tell the compiler to write an uninstaller and to look for a "Uninstall" section 
WriteUninstaller $INSTDIR\Uninstall.exe

; start menu items
CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\CadPlayG.lnk" "$INSTDIR\CadPlayG.exe"
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\Uninstall.exe"

; registry keys for add/remove in control panel
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "DisplayName" "${PRODUCT_NAME} ${DESCRIPTION}"
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "UninstallString" "$\"$INSTDIR\Uninstall.exe$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "QuietUninstallString" "$\"$INSTDIR\Uninstall.exe$\" /S"
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "InstallLocation" "$\"$INSTDIR$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "DisplayIcon" "$\"$INSTDIR\CadPlayG.exe$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "Publisher" "$\"${COMPANYNAME}$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "HelpLink" "$\"${HELPURL}$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "URLUpdateInfo" "$\"${UPDATEURL}$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "URLInfoAbout" "$\"${ABOUTURL}$\""
WriteRegStr HKLM "${REG_UNINST}\${PRODUCT_NAME}" "DisplayVersion" "$\"${PRODUCT_VERSION}$\""
WriteRegDWORD HKLM "${REG_UNINST}\${PRODUCT_NAME}" "VersionMajor" ${VERSIONMAJOR}
WriteRegDWORD HKLM "${REG_UNINST}\${PRODUCT_NAME}" "VersionMinor" ${VERSIONMINOR}
# There is no option for modifying or repairing the install
WriteRegDWORD HKLM "${REG_UNINST}\${PRODUCT_NAME}" "NoModify" 1
WriteRegDWORD HKLM "${REG_UNINST}\${PRODUCT_NAME}" "NoRepair" 1
# Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size
WriteRegDWORD HKLM "${REG_UNINST}\${PRODUCT_NAME}" "EstimatedSize" ${INSTALLSIZE}

SectionEnd ; end the section
;================================



;--------------------------------
; The uninstall section

Section			"Uninstall"

; files in install folder
Delete $INSTDIR\Uninstall.exe

Delete $INSTDIR\${PRODUCT_BASE}-${PRODUCT_VERSION}.tar.gz


Delete $INSTDIR\help\cpg.qhc
RMDir $INSTDIR\help

Delete $INSTDIR\assistant.exe
Delete $INSTDIR\libgcc_s_dw2-1.dll
Delete $INSTDIR\libcpg.a
Delete $INSTDIR\mingwm10.dll
Delete $INSTDIR\libstdc++-6.dll

Delete $INSTDIR\CadPlayG${DBG_MARKER}.exe
Delete $INSTDIR\cpg${DBG_MARKER}.dll


; Qt libraries
Delete $INSTDIR\QtCLucene${DBG_LETTER}4.dll
Delete $INSTDIR\QtCore${DBG_LETTER}4.dll
Delete $INSTDIR\QtGui${DBG_LETTER}4.dll
Delete $INSTDIR\QtHelp${DBG_LETTER}4.dll
Delete $INSTDIR\QtNetwork${DBG_LETTER}4.dll
Delete $INSTDIR\QtSql${DBG_LETTER}4.dll
Delete $INSTDIR\QtWebKit${DBG_LETTER}4.dll

Delete $INSTDIR\COPYING
Delete $INSTDIR\NEWS

Delete $INSTDIR\lang\CadPlayG_ro_RO.qm
Delete $INSTDIR\lang\dyn_cpg_ro_RO.qm
Delete $INSTDIR\lang\sta_cpg_ro_RO.qm
RMDir $INSTDIR\lang


RMDir $INSTDIR

; start menu items
Delete "$SMPROGRAMS\${PRODUCT_NAME}\CadPlayG.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
RMDir "$SMPROGRAMS\${PRODUCT_NAME}"

; registry keys for add/remove in control panel
DeleteRegKey HKEY_CURRENT_USER "SOFTWARE\TNick\${PRODUCT_NAME}"
DeleteRegKey HKEY_LOCAL_MACHINE "${REG_UNINST}\${PRODUCT_NAME}"



SectionEnd ; end the section
;================================

